package com.example.admindatasource.model.vo;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.Base64;
import java.util.Objects;
import java.util.Optional;

public class SysUploadFileVO {


    @ApiModelProperty(value = "主键")
    private Integer id;

    @ApiModelProperty(value = "文件名称")
    private String fileName;

    @ApiModelProperty(value = "文件原始名称")
    private String originFileName;

    @ApiModelProperty(value = "请求地址")
    private String location;

    @ApiModelProperty(value = "上传地址  （相对路径）")
    @JsonIgnore
    private String filePath;

    @ApiModelProperty(value = "文件类型")
    private String type;

    @ApiModelProperty(value = "模块名称")
    private String module;

    @ApiModelProperty(value = "文件大小 单位byte")
    private long size;

    @ApiModelProperty("状态")
    private Integer status;

    @ApiModelProperty(value = "状态 1原始文件 2 追加的文件")
    @TableField("origin_append_status")
    private Integer originAppendStatus;

    @ApiModelProperty(value = "创建时间")
    private LocalDateTime createTime;

    @ApiModelProperty(value = "创建人")
    private Integer createId;

    @ApiModelProperty("文件base64格式数据")
    private String data;

    @ApiModelProperty("是否新上传")
    private boolean flag;

    public static String toKB(long size) {
        String format = String.format("%.2f", Optional.ofNullable(size).orElse(0L) / 1024.0);
        return format + "KB";
    }

    public static String toMB(long size) {
        String format = String.format("%.2f", Optional.ofNullable(size).orElse(0L) / 1024.0 / 1024.0);
        return format + "MB";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        SysUploadFileVO that = (SysUploadFileVO) o;
        return Objects.equals(id, that.id);
    }

    public InputStream inputStreamFromBase64() {
        String originalFilename = this.getFileName();
        String module = this.getModule();

//        byte[] bytes = Base64.getDecoder().decode(this.getData());
        String data = this.getData();
        if (data.contains("data:")) {
            int start = data.indexOf(",");
            data = data.substring(start + 1);
        }
        final Base64.Decoder decoder = Base64.getDecoder();
        data = data.replaceAll("\r|\n", "");
        data = data.trim();
        byte[] bytes = decoder.decode(data);
        return new ByteArrayInputStream(bytes);
    }

    /*  */

    public Integer getOriginAppendStatus() {
        return originAppendStatus;
    }

    public void setOriginAppendStatus(Integer originAppendStatus) {
        this.originAppendStatus = originAppendStatus;
    }

    public boolean getFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getOriginFileName() {
        return originFileName;
    }

    public void setOriginFileName(String originFileName) {
        this.originFileName = originFileName;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getModule() {
        return module;
    }

    public void setModule(String module) {
        this.module = module;
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    public Integer getCreateId() {
        return createId;
    }

    public void setCreateId(Integer createId) {
        this.createId = createId;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }
}
